<?php
/**
 * 虚拟e卡前台
 * Created by PhpStorm.
 * User: zym/wika
 * Date: 2017/4/11
 * Time: 10:41
 */
class StoredValueAction extends CommonAction
{
    public $storedValue;
    public $storedValueId;
    public $storedValueBind;
    public $browser;

    public function __construct()
    {
        $otherAction = array("notify");
        //支付回调不需要走构造函数
        if (!in_array(strtolower(ACTION_NAME), $otherAction)) {
            $condition = array(
                'status' => 1
            );
            $account_id = $_GET['account_id'];
            if ($account_id) {
                $condition['account_id'] = $account_id;
            }
            $storedValueId = $_GET['stored_value_id'];
            if ($storedValueId) {
                $condition['stored_value_id'] = $storedValueId;
            }
            $storedValue = M('StoredValue')->where($condition)->find();
            if (!$storedValue) {
                $this->error("储值卡账户不存在");
            }
            $this->storedValue = $storedValue;
            $this->storedValueId = $this->storedValue['stored_value_id'];
            $this->accountId = $this->storedValue['account_id'];
            $this->login('StoredValue');
            $this->assign('stored_value_id', $this->storedValueId);
            $this->assign('storedValue', $this->storedValue);
            $this->assign('account_id', $this->accountId);

            //通过浏览器调取不用的用户信息和支付信息
            if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') != false) {
                $this->browser = 1;
            } elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Alipay') != false) {
                $this->browser = 2;
            }
            //使用主键查询笔使用条件查询性能更快，所以保存session
            if (session('stored_value_id')) {
                $condition = array(
                    'stored_value_id' => session('stored_value_id')
                );
            } else {
                $condition = array(
                    'stored_value_id' => $this->storedValueId,
                    'user_id' => $this->userId,
                );
            }
            $dataBind = M('StoredValueBind')->where($condition)->find();
            $dataUser = M('User')->field('number,real_name,mobile,type,status,update_time')->where(array('user_id' => $this->userId))->find();
            if ($dataBind) { //存在数据
                if ($dataUser['update_time'] > $dataBind['update_time']) { //如果用户信息最近有更新
                    //权限判定
                    $this->noPower($dataUser);

                    //学号有更改
                    if($dataBind['number'] != $dataUser['number']){
                        $dataBind['number'] = $dataUser['number'];
                    }

                    //姓名有更改
                    if ($dataBind['user_name'] != $dataUser['real_name']) {
                        $dataBind['user_name'] = $dataUser['real_name'];
                    }
                    $dataBind['mobile'] = $dataUser['mobile'];
                    $dataBind['type'] = $dataUser['type'];
                    $dataBind['update_time'] = $dataUser['update_time'];
                    M('StoredValueBind')->save($dataBind);
                }
                $this->storedValueBind = $dataBind;
            } else { //不存在数据,添加一条
                //权限判定
                $this->noPower($dataUser);
                //添加数据
                $dataBind = array(
                    'stored_value_id' => $this->storedValueId,
                    'user_id' => $this->userId,
                    'user_name' => $dataUser['real_name'],
                    'number' => $dataUser['number'],
                    'mobile' => $dataUser['mobile'],
                    'type' => $dataUser['type'],
                    'create_time' => time(),
                    'update_time' => $dataUser['update_time']
                );
                $res = M('StoredValueBind')->add($dataBind);
                if ($res) {
                    $dataBind['bind_id'] = $res;
                }
                $this->storedValueBind = $dataBind;
            }
        }
    }

    /**
     * 访问权限判断
     */
    public function noPower($dataUser){
        //身份未认证
        if ($dataUser['status'] != 2) {
            $this->error("对不起!您的身份未认证通过，请联系管理员");
        }
        $guestAlias = M('Account')->where(array('account_id'=>$this->accountId,"status"=>1))->getField("guest_alias");
        $guestAlias = $guestAlias ? $guestAlias:"来宾";
        //来宾没有权限
        if ($dataUser['type'] == 2) {
            $this->error("抱歉! ".$guestAlias."身份没有访问权限");
        }

        //学号为空
        if (!$dataUser['number']) {
            $this->error("对不起!您未绑定工号，请联系管理员");
        }
        //学号被绑定
        $hasCount = M('StoredValueBind')->where(array("number" => $dataUser['number'], "stored_value_id" => $this->storedValueId, "status" => 1))->count();
        if ($hasCount > 0) {
            $this->error("对不起!此工号已被绑定");
        }
    }


    /**
     * 首页
     */
    public function index()
    {
        //未绑卡
        if ($this->storedValueBind['status'] == 0 || $this->storedValueBind['status'] == 2) {
            $this->noBind();
            exit();
        }
        //已绑卡
        $this->hasBind();
    }

    /**
     * 未绑卡跳转
     */
    public function noBind(){
        $this->redirect("/base/StoredValue/noCard", array('stored_value_id' => $this->storedValueId));
    }

    /**
     * 已绑卡跳转
     */
    public function hasBind(){
        $this->redirect("/base/StoredValue/payment", array('stored_value_id' => $this->storedValueId));
    }

    /**
     * 未绑定卡页面
     */
    public function noCard()
    {
        //已绑卡
        if ($this->storedValueBind['status'] == 1) {
            $this->hasBind();
            exit();
        }
        $this->display();
    }

    /**
     * 绑卡页面
     */
    public function bound()
    {
        session('parames',null);
        //已绑卡
        if ($this->storedValueBind['status'] == 1) {
            $this->hasBind();
            exit();
        }
        if($_GET['is_restaurant']){
            session('parames',$_GET['parames']);
        }
        $this->assign('bind', $this->storedValueBind);
        $this->display();
    }

    /**
     * 发送短信
     */
    public function sendText()
    {
        $user_mobile = $this->_request('mobile');
        if (!$user_mobile) {
            $this->ajaxReturn(array('status' => 'F', 'message' => '手机号码不能为空'));
        }
        $code = session('code');
        $time = session('last_time');
        $gap = time() - $time;
        if (!$code || $gap > 60 * 10) {
            $code = rand(100000, 999999);
            session('code', $code);
            session('last_time', time());
        }
        $ch = curl_init();
        $data = array(
            'username' => 'yujiao',
            'password' => '111111',
            'mobile' => $user_mobile,
            'content' => '【虚拟e卡】您的注册验证码为' . $code . ',有效时间十分钟',
            'planTime' => ''
        );
        curl_setopt($ch, CURLOPT, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL, 'http://115.29.44.189:8080/sms/smsInterface.do');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $result = curl_exec($ch);
        if ($result) {
            session('mobile', $user_mobile);
            $this->ajaxReturn(array('status' => 'S', 'message' => '验证码已发送'));
        } else {
            $this->ajaxReturn(array('status' => 'F', 'message' => '验证码发送失败，请重试'));
        }
    }

    /**
     * 绑定操作
     */
    public function doBound()
    {
        if ($this->storedValueBind['status'] == 1) {
            $this->ajaxReturn(array('status' => 'S', 'message' => '已成功绑卡，无需再次绑定'));
        }
        $code = $_POST['code'];
        if (!session('last_time')) {
            $this->ajaxReturn(array('status' => 'F', 'message' => '请先获取手机验证码'));
        }
        if (session('last_time') < time() - 60 * 10) {
            $this->ajaxReturn(array('status' => 'F', 'message' => '验证码已过期，请重新获取'));
        }
        if ($code != session("code")) {
            $this->ajaxReturn(array('status' => 'F', 'message' => '验证码错误'));
        }
        $data = M('storedValueBind')->create();
        if (!$data['bind_id']) {
            $this->ajaxReturn(array('status' => 'F', 'message' => '参数错误'));
        }
        if ($data['mobile'] != session("mobile")) {
            $this->ajaxReturn(array('status' => 'F', 'message' => '请填写接收验证码的手机号'));
        }
        $data['status'] = 1;
        $data['bind_time'] = time();
        $res = M("storedValueBind")->save($data);
        if ($res) {
            $this->storedValueBind = array_merge($this->storedValueBind, $data);
            //更新用户表的电话号码
            //M('User')->where(array("user_id" => $this->userId))->save(array("mobile" => $data['mobile']));
            session('code', null);
            session('last_time', null);
            if(session('parames')) {
                $parames = base64_decode(session('parames'));
                $paramesArr = explode("||",$parames);
                $url ="/index.php?g=base&m=restaurant&a=confirmorders&res_id=".$paramesArr[0]."&order_id=".$paramesArr[1]."&pay_type=".$paramesArr[2];
                $this->ajaxReturn(array('status' => 'BS', 'message' => '绑定成功', 'url'=> $url));
            } else {
                $this->ajaxReturn(array('status' => 'S', 'message' => '绑定成功'));
            }
        } else {
            $this->ajaxReturn(array('status' => 'F', 'message' => '绑定失败'));
        }
    }

    /**
     * 用户中心
     */
    public function personalCenter()
    {
        //未绑卡
        if ($this->storedValueBind['status'] == 0 || $this->storedValueBind['status'] == 2) {
            $this->noBind();
            exit();
        }

        $this->assign("bind", $this->storedValueBind);
        $this->display();
    }

    /**
     * 充值
     */
    public function pay()
    {
        //未绑卡
        if ($this->storedValueBind['status'] == 0 || $this->storedValueBind['status'] == 2) {
            $this->noBind();
            exit();
        }
        $dataAmount = M('StoredValueAmount')->where(array('stored_value_id' => $this->storedValueId, 'cut_off' => 0))->order('sort asc,id desc')->select();
        $this->assign("dataAmount", $dataAmount);
        $this->assign('bind', $this->storedValueBind);
        $this->display();
    }

    /**
     * ajax创建订单
     */
    public function doCreateOrder()
    {
        $order_id = 0;
        // 生成订单号
        $orderSn = $this->getOrderSn('StoredValue');
        if ($orderSn['status'] == 'F') {
            $this->ajaxReturn(array('status' => 'F', 'message' => $orderSn['message']));
        }
        $order = array(
            'account_id' => $this->accountId,
            'stored_value_id' => $this->storedValueId,
            'bind_id' => $this->storedValueBind['bind_id'],
            'order_sn' => $orderSn['data'],
            'user_id' => $this->storedValueBind['user_id'],
            'number' => $this->storedValueBind['number'],
            'user_name' => $this->storedValueBind['user_name'],
            'pay_type' => $this->browser == 2 ? 2 : 1,
            'alipay_pay_id' => session("alipayId"),
            'alipay_user_id' => session("alipayUserId"),
            'wechat_pay_id' => session("wechatId"),
            'wechat_user_id' => session("wechatUserId"),
            'create_time' => time(),
            'order_amount' => $_POST['amount'] ? $_POST['amount'] : 0,
            'goods_amount' => $_POST['amount'] ? $_POST['amount'] : 0
        );
        if ($this->browser == 1) {
            $openId = M('WechatUser')->where(array('wechat_user_id' => session("wechatUserId")))->getField("openid");
            $order['openid'] = $openId;
        }
        wLog(date('Y-m-d H:i:s').' order: '.var_export($order,true),"storedvalue");
        $res = M('StoredValueRecharge')->add($order);
        if ($res) {
            $order['order_id'] = $res;
        } else {
            $this->ajaxReturn(array('status' => 'F', 'message' => '创建订单出错'));
        }
        //支付
        if ($order['order_amount'] > 0) {
            if ($this->browser == 2) {
                //服务窗支付宝手机网站支付
                $returnUrl = C('site_url') . U('base/StoredValue/payrecord', array('one_card_id' => $this->oneCardId));
                //支付宝异步通知地址
                $notifyUrl = C('site_url') . U('alipay/AlipayNotify/notify');
                $prepayData = $this->newPay($this->accountId, $order['order_sn'], '虚拟e卡充值', $notifyUrl, $returnUrl);
                if (is_array($prepayData) && $prepayData['status'] == 'F') {
                    $this->ajaxReturn(array('status' => 'F', 'message' => $prepayData['message']));
                }
                $result = $prepayData->windowPay();
                $this->ajaxReturn(array('status' => 'AS', 'message' => '调起支付宝成功', 'url' => $result));
            } else {
                $prepayData = $this->newPay($this->accountId, $order['order_sn'], '虚拟e卡充值', '', '');
                if (is_array($prepayData) && $prepayData['status'] == 'F') {
                    $this->ajaxReturn(array('status' => 'F', 'message' => $prepayData['message']));
                }
                $result = $prepayData->windowPay();
                wLog(date('Y-m-d H:i:s').' result: '.var_export($result,true),"storedvalue");
                if ($result['status'] == 'S') {
                    $this->ajaxReturn(array('status' => 'WS', 'message' => '调起微信成功', 'url' => $result['data'], 'order_id' => $order['order_id']));
                } else {
                    $this->ajaxReturn(array('status' => 'F', 'message' => $result['message']));
                }
            }
        } else {
            $this->ajaxReturn(array('status' => 'S', 'message' => '支付金额为0', 'order_id' => $order_id));
        }
    }

    /**
     * ajax付款成功（金额 = 0用到和微信跳转用到）
     */
    public function paySuccess($order_id = 0, $account_id = 0)
    {
        if (!$order_id) {
            $order_id = $this->_get('order_id');
        }
        if (!$order_id) {
            $this->ajaxReturn(array('status' => 'F', 'message' => '充值失败'));
        }

        $order = M('StoredValueRecharge')->where(array('order_id' => $order_id))->find();
        $url = "/index.php?g=base&m=StoredValue&a=pay&stored_value_id=".$this->storedValueId;
        //订单价格为0
        if ($order['order_amount'] == 0) {
            $update = array(
                'order_id' => $order['order_id'],
                'status' => 1,
                'pay_time' => time(),
                'is_pay_notify' => 1,
                'pay_notify_time' => time(),
            );
            if ($this->browser == 2) {
                $update['pay_type'] = 2;
            }
            if ($order['status'] == 0 && $order['is_pay_notify'] == 0) {
                M('StoredValueRecharge')->save($update);
            }
            //推送充值成功消息
            $this->ajaxReturn(array('status' => 'S', 'message' => '充值成功', 'url' => $url));
        } elseif ($order['order_amount'] > 0 && $order['pay_type'] == 1) {
            if ($order['status'] == 0) {
                sleep(1);
                //获取充值状态
                $result = $this->rechargeSuccess($order_id);
                if($result['status'] == 'S') {
                    //推送充值成功消息
                    $this->ajaxReturn(array('status' => 'S', 'message' => '充值成功', 'url' => $url));
                } else {
                    $url = U('base/StoredValue/payrecord', array('stored_value_id' => $order['stored_value_id']));
                    $this->ajaxReturn(array('status' => 'S', 'message' => $result['message'], 'url' => $url));
                }
            } elseif ($order['status'] == 1) {
                //推送充值成功消息
                $this->ajaxReturn(array('status' => 'S', 'message' => '充值成功', 'url' => $url));
            } else {
                $url = U('base/StoredValue/payrecord', array('one_card_id' => $this->storedValue['stored_value_id']));
                $this->ajaxReturn(array('status' => 'S', 'message' => '充值失败', 'url' => $url));
            }
        }
    }

    /**
     * 判断是否支付成功
     */
    public function rechargeSuccess($order_id,$count = 15){
        if($count <= 0 ){
            return array("status"=>"F","message"=>"充值失败");
        }
        $order = M('StoredValueRecharge')->where(array('order_id' => $order_id))->find();
        if($order['status'] == 0) {
            sleep(1);
            $count--;
            $this->rechargeSuccess($order_id,$count);
        } elseif($order['status'] == 1){
            return array("status"=>"S","message"=>"充值成功");
        }
    }

    /**
     * 支付回调
     */
    public function notify($order_id, $account_id = 0)
    {
        $order = M('StoredValueRecharge')->where(array('order_id' => $order_id))->find();
        //已付款成功。金额大于0，微信将在paysucces再处理一遍（如果未充值成功的话）
        if ($order['status'] == 1 && $order['order_amount'] > 0) {
            //更新余额
            $order = M('StoredValueRecharge')->where(array('order_id'=>$order_id))->find();
            $bind_id = $order['bind_id'];
            $amount = $order['order_amount'];
            //获取当前余额
            $balance = M('StoredValueBind') -> where(array('bind_id'=>$bind_id))->getField('balance');
            $balance = $balance + $amount;
            //增加订单余额流水记录
            M('StoredValueRecharge')->where(array('order_id'=>$order_id))->setField('balance',$balance);
            //增加用户余额
            M('StoredValueBind')->where(array('bind_id'=>$bind_id))->setInc('balance',$amount);
            //推送充值成功消息

        }
    }

    /**
     * 充值记录
     */
    public function payRecord()
    {
        //未绑卡
        if ($this->storedValueBind['status'] == 0 || $this->storedValueBind['status'] == 2) {
            $this->noBind();
            exit();
        }
        $data = M('StoredValueRecharge')->where(array('bind_id' => $this->storedValueBind['bind_id'],'status'=>1))->order("order_id desc")->select();
        $this->view($data);
    }

    /**
     * 补贴记录
     */
    public function subsidyLog()
    {
        //未绑卡
        if ($this->storedValueBind['status'] == 0 || $this->storedValueBind['status'] == 2) {
            $this->noBind();
            exit();
        }
        $data = M('StoredValueSubsidy')->where(array('bind_id' => $this->storedValueBind['bind_id']))->order("order_id desc")->select();
        $dataSubAccount = M('SubAccount')->where(array('account_id' => $this->accountId))->getField('sub_account_id,sub_account_name');
        foreach ($data as $k=>$v){
            if(array_key_exists($v['sub_account_id'],$dataSubAccount)){
                $data[$k]['sub_account_name'] = $dataSubAccount[$v['sub_account_id']];
            }
        }
        $this->view($data);
    }

    /**
     * 消费记录
     */
    public function orderLog()
    {
        //未绑卡
        if ($this->storedValueBind['status'] == 0 || $this->storedValueBind['status'] == 2) {
            $this->noBind();
            exit();
        }
        $data = M('StoredValueOrder')->where(array('bind_id' => $this->storedValueBind['bind_id'],'status'=>1))->order("order_id desc")->select();
        $dataDevice = M('CardPayPartner')->where(array('account_id'=>$this->accountId,'cut_off'=>0))->getField("device_id,terminal_name");
        foreach ($data as $k=>$v){
            if($v['order_type'] == 1){
                $data[$k]['terminal_name'] = "在线订餐";
            } elseif (array_key_exists($v['device_id'],$dataDevice)){
                $data[$k]['terminal_name'] = $dataDevice[$v['device_id']];
            }
        }
        $this->view($data);
    }

    /**
     * 虚拟卡付款界面
     */
    public function payment(){
        //未绑卡
        if ($this->storedValueBind['status'] == 0 || $this->storedValueBind['status'] == 2) {
            $this->noBind();
            exit();
        }
        C('REDIS_DEF_DB',9);
        $redis = RedisService::connectRedis();
        $key = 'string_start_time_'.$this->storedValueBind['bind_id'];
        $redis->set($key,time());
        $code_number = $this->getCode();
        $this->assign('code_number',$code_number);
        $this->display();
    }

    /**
     * 生成15位付款码
     * @return string
     */
    public function getCode(){
        //首位8
        $code = '8';
        //8位主键id
        $bind_id = $this->storedValueBind['bind_id'];
        $code .= str_pad($bind_id,8,'0');
        //6位随机数
        $code .= rand(100000,999999);
        $res = M('StoredValueBind')->where(array('bind_id'=>$bind_id))->save(array('code_number'=>$code,'is_code_use'=>0));
        if($res){
            return $code;
        } else {
            $this->error("生成付款码错误");
        }
    }

    /**
     * 生成二维码
     */
    public function qrcode($code_number)
    {
        vendor('phpqrcode.phpqrcode');
        $errorCorrectionLevel = 2;//容错级别
        $matrixPointSize = 20;//生成图片大小
        //生成二维码图片
        //QRcode::png($code_number, false, $errorCorrectionLevel, $matrixPointSize, 2);

        //生成二维码图片
        QRcode::png($code_number, './uploads/StoredValueQRcode/qrcode.png', $errorCorrectionLevel, $matrixPointSize, 2);
        $logo = './assets/base/StoredValue/images/logo3.png';//准备好的logo图片
        $QR = './uploads/StoredValueQRcode/qrcode.png';//已经生成的原始二维码图

        if ($logo !== FALSE) {
            $QR = imagecreatefromstring(file_get_contents($QR));
            $logo = imagecreatefromstring(file_get_contents($logo));
            $QR_width = imagesx($QR);//二维码图片宽度
            $QR_height = imagesy($QR);//二维码图片高度
            $logo_width = imagesx($logo);//logo图片宽度
            $logo_height = imagesy($logo);//logo图片高度
            $logo_qr_width = $QR_width / 5;
            $scale = $logo_width / $logo_qr_width;
            $logo_qr_height = $logo_height / $scale;
            $from_width = ($QR_width - $logo_qr_width) / 2;
            //重新组合图片并调整大小
            imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
        }
        //输出图片
        Header("Content-type: image/png");
        ImagePng($QR);
    }

    /**
     * 生成条形码
     */
    public function barcode($code_number,$dirname = '',$name=''){
        require_once(VENDOR_PATH.'barcode/class/BCGFontFile.php');
        require_once(VENDOR_PATH.'barcode/class/BCGColor.php');
        require_once(VENDOR_PATH.'barcode/class/BCGDrawing.php');
        $codebar = 'BCGcode128';
        require_once(VENDOR_PATH.'barcode/class/BCGcode128.barcode.php');
        $font = new BCGFontFile(VENDOR_PATH.'barcode/font/Arial.ttf', 24);
        // The arguments are R, G, B for color.
        $color_black = new BCGColor(0, 0, 0);
        $color_white = new BCGColor(255, 255, 255);
        $drawException = null;
        try {
            $code = new $codebar();//实例化对应的编码格式
            $code->setScale(4); // Resolution
            $code->setThickness(23); // Thickness
            $code->setForegroundColor($color_black); // Color of bars
            $code->setBackgroundColor($color_white); // Color of spaces
            $code->setFont($font); // Font (or 0) 0不展示条码下的数据
            $text = $code_number; //条形码将要数据的内容
            $code->parse($text);
        } catch(Exception $exception) {
            $drawException = $exception;
        }

        /* Here is the list of the arguments
        1 - Filename (empty : display on screen)
        2 - Background color */
        if($dirname && $name){
            $drawing = new BCGDrawing($dirname.$name, $color_white);
        } else {
            $drawing = new BCGDrawing('', $color_white);
        }

        if($drawException) {
            $drawing->drawException($drawException);
        } else {
            $drawing->setBarcode($code);
            $drawing->draw();
        }
        //header('Content-Type: image/png');
        $drawing->finish(BCGDrawing::IMG_FORMAT_PNG);
    }

    /**
     * 查询订单是否支付成功
     */
    public function doSelectOrderPay(){
        $bind_id = $this->storedValueBind['bind_id'];

        //获取该用户未支付成功的订单
        $order_id = $_POST['order_id'];
        if($order_id) {
            $count = M('StoredValueOrder')->where(array('order_id' => $order_id, 'status' => 1))->count();
            if($count > 0){
                //解除已使用
                M('StoredValueBind')->where(array('bind_id' => $bind_id))->setField('is_code_use', 0);
                //支付成功
                $this->ajaxReturn(array('status' => 'S','order_id'=>$order_id));
            } else {
                //等待用户支付
                $this->ajaxReturn(array('status' => 'PS','order_id'=>$order_id));
            }
        } else {
            C('REDIS_DEF_DB',9);
            $redis = RedisService::connectRedis();
            //获取用户进入时的时间
            $key = 'string_start_time_' . $bind_id;
            $start_time = $redis->get($key);
            //注意根据时间查找一定要是同一台服务器，要不然时间不统一，
            $order = M('StoredValueOrder')->where(array('bind_id' => $bind_id, 'create_time' => array('egt', $start_time)))->field('order_id,status')->find();
            if ($order) {
                //清空用户进入时间
                $redis->del($key);
                if($order['status'] == 1) {
                    //解除已使用
                    M('StoredValueBind')->where(array('bind_id' => $bind_id))->setField('is_code_use', 0);
                    //支付成功
                    $this->ajaxReturn(array('status' => 'S','order_id'=>$order['order_id']));
                } else {
                    //等待用户支付
                    $this->ajaxReturn(array('status' => 'PS', 'order_id' => $order['order_id']));
                }
            } else {
                //未找到待支付订单
                $this->ajaxReturn(array('status' => 'F'));
            }
        }
    }

    /**
     * 虚拟卡付款成功
     */
    public function paymentSuccess(){
        $order_id = $_GET['order_id'];
        if(!$order_id){
            $this->error("参数错误");
        }
        $order = M('StoredValueOrder')->where(array('order_id'=>$order_id))->field('device_id,amount')->find();
        $device_id = $order['device_id'];
        $terminal_name = M('CardPayPartner')->where(array('device_id'=>$device_id))->getField('terminal_name');
        $order['terminal_name']=$terminal_name;
        $this->view($order,'paymentsuccess');
    }
}